package com.haojiang.mh.remote.adapter

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.remote.bean.BleRssiDevice
import com.haojiang.mh.remote.callback.IBleItemClickLisenter
import com.haojiang.mh.remote.enums.EConnect
import com.haojiang.mh.remote.ui.dialog.ModifyDialogFrag
import com.haojiang.mh.remote.utils.BleUtil.Companion.ble
import com.haojiang.mh.remote.utils.BleUtil.Companion.lastConnect
import com.haojiang.mh.remote.utils.Util.Companion.clickItemDelay
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import com.haojiang.mh.remote.widget.BleSIngleItemView
import kotlinx.android.synthetic.main.item_ble_single.view.*

class BleSingleDialogFragAdapter(var fm: FragmentActivity, list: MutableList<BleRssiDevice>) :
    RecyclerView.Adapter<BleSingleDialogFragAdapter.BleDialogFragViewHolder>() {

    private var dataList: MutableList<BleRssiDevice> = list
    val viewList = ArrayList<BleSIngleItemView>()
    var iOnBleItemClickLisenter: IBleItemClickLisenter? = null


    //初始化数据
    fun initData() {
        this.viewList.forEach {
            it.isSelect = false
        }
        this.viewList.clear()
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BleDialogFragViewHolder {
        return BleDialogFragViewHolder(BleSIngleItemView(parent.context))       //BleItemView是layout文件
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: BleDialogFragViewHolder, position: Int) {
        val bleRssiDevice = dataList.get(position)
        val itemView = holder.itemView as BleSIngleItemView
        if (!viewList.contains(itemView)) {
            itemView.bleRssiDevice = bleRssiDevice
            viewList.add(itemView)

//            LogUtil.e("蓝牙 = " + bleRssiDevice.toString())

            itemView.setData(bleRssiDevice) //设置rssi和设备别名

            //修改设备名称
            itemView.ivModify.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    ble.stopScan()
                    ModifyDialogFrag.show(fm, bleRssiDevice.bleAddress, bleRssiDevice.bleName, bleRssiDevice.deviceAlias!!)
                }
            })

            //l连接设备按钮
            itemView.tvTest.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    ble.stopScan()
                    clickItemDelay(v!!, 3000)
                    playSound()
                    val bleDeivceList = ArrayList<BleRssiDevice>()
                    bleDeivceList.add(dataList.get(position))
                    lastConnect.connectType = EConnect.CONNECT_TEST.data
                    iOnBleItemClickLisenter?.onClick(bleDeivceList, EConnect.CONNECT_TEST.data)
                }
            })

            //l连接设备按钮
            itemView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    clickItemDelay(v!!, 3000)
                    ble.stopScan()
                    playSound()
                    val bleDeivceList = ArrayList<BleRssiDevice>()
                    bleDeivceList.add(dataList.get(position))
                    iOnBleItemClickLisenter?.onClick(bleDeivceList, EConnect.CONNECT_SINGLE.data)
                }
            })
        }

        //如果是组选择子选项
//        setSelectType(itemView)
    }

//    private fun setSelectType(itemView: BleControlItemView) {
////        when (groupType) {
//        when (lastGroupType) {
//            GROUP_TYPE_SINGLE,
//            GROUP_TYPE_SPLIT -> {
//                itemView.ivSelector.visibility = View.VISIBLE
//                //单个选择
//                itemView.ivSelector.setOnClickListener(object : View.OnClickListener {
//                    override fun onClick(v: View?) {
//                        viewList.forEach {
//                            if (it.bleRssiDevice!!.bleAddress.equals(itemView.bleRssiDevice!!.bleAddress)) {
//                                it.isSelect = true
//                                it.ivSelector.setImageResource(R.drawable.ic_select)
//                            } else {
//                                it.isSelect = false
//                                it.ivSelector.setImageResource(R.drawable.ic_unselect)
//                            }
//                        }
//                    }
//                })
//            }
//
//            GROUP_TYPE_MULTIPLE -> {
//                itemView.ivSelector.visibility = View.VISIBLE
//                //多个选择
//                itemView.ivSelector.setOnClickListener(object : View.OnClickListener {
//                    override fun onClick(v: View?) {
//                        viewList.forEach {
//                            if (it.bleRssiDevice!!.bleAddress.equals(itemView.bleRssiDevice!!.bleAddress)) {
//                                if (it.isSelect) {
//                                    it.isSelect = false
//                                    it.ivSelector.setImageResource(R.drawable.ic_unselect)
//                                } else {
//                                    it.isSelect = true
//                                    it.ivSelector.setImageResource(R.drawable.ic_select)
//                                }
//                            }
//                        }
//                    }
//                })
//            }
//        }
//    }


//    fun dataRssiRefresh() {
//        Collections.sort(dataList)    //排序
//        notifyDataSetChanged()
//    }

    fun modifyItem(bleAddress: String, deviceAlias: String) {
//            LogUtil.e(bleAddress + "=deviceAlias=" + deviceAlias)
//        for (i: Int in 0 until dataList.size) {
//            LogUtil.e(dataList.get(i).bleName + "=dataList = deviceAlias=" + dataList.get(i).deviceAlias)
//            if (dataList.get(i).bleAddress.equals(bleAddress)) {
//                dataList.get(i).deviceAlias = deviceAlias
//                notifyItemChanged(i)
//                break
//            }
//        }
        for (i in 0 until viewList.size) {
            if (viewList.get(i).bleRssiDevice!!.bleAddress.equals(bleAddress)) {
                viewList.get(i).tvDevName.setText(deviceAlias)
                break
            }
        }
    }

//    fun getSelectList(): ArrayList<GroupValue> {
//        val groupValueLIst = ArrayList<GroupValue>()
//        viewList.forEach {
//            if (it.isSelect) {
//                groupValueLIst.add(GroupValue(it.bleRssiDevice!!))
//            }
//        }
//        return groupValueLIst
//    }


    class BleDialogFragViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }
}